package com.sd.jd.jdk_history;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/**
 * java10 <br/>
 * 1、Http Client {@link Test1}<br/>
 * 4、Optional {@link Test4}<br/>
 * 5、String{@link Test5} <br/>
 * 6、String{@link Test6} <br/>
 */
public class Jd11 {

    public static class Test1 {
        public static void main(String[] args) throws Exception {
            {
                var request = HttpRequest.newBuilder()
                        .uri(URI.create("http://jsonplaceholder.typicode.com/users"))
                        .GET()
                        .build();
                var client = HttpClient.newHttpClient();
                HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
                System.out.println(response.body());
            }

            // 可以异步执行相同的请求。调用sendAsync不会阻塞当前线程，而是返回一个CompletableFuture来构造异步操作流水线。
            {
                var request = HttpRequest.newBuilder()
                        .uri(URI.create("http://jsonplaceholder.typicode.com/users"))
                        .build();
                var client = HttpClient.newHttpClient();
                CompletableFuture<Void> voidCompletableFuture = client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
                        .thenApply(HttpResponse::body)
                        .thenAccept(System.out::println);

            }

        }
    }

    public static class Test4 {
        public static void main(String[] args) {
            String foo = Optional.of("foo").orElseThrow(); // foo

            long foo1 = Optional.of("foo").stream().count(); // 1

            Object o = Optional.ofNullable(null)
                    .or(() -> Optional.of("fallback"))
                    .get(); // fallback

        }
    }

    public static class Test5 {
        public static void main(String[] args) {
            " ".isBlank();                // true

            " Foo Bar ".strip();          // "Foo Bar"

            " Foo Bar ".stripTrailing();  // " Foo Bar"

            " Foo Bar ".stripLeading();   // "Foo Bar "

            "Java".repeat(3);             // "JavaJavaJava"

            long count = "A\nB\nC".lines().count();// 3
            System.out.println(count);

            // 注意你可能觉得 strip 和 trim 方法一样，一般使用的话差不多，但实际上他们不一样，有所区别：
            // trim() 可以去除字符串前后的半角空白字符
            // strip() 可以去除字符串前后的全角和半角空白字符

            String test1="测试、u0020";//半角 unicode
            System.out.println(test1.trim().length());//2
            System.out.println(test1.strip().length());//2

            String test2="测试、u3000";//全角 unicode
            System.out.println(test2.trim().length());//3
            System.out.println(test2.strip().length());//2

            String test3="测试 ";//半角空白字符
            System.out.println(test3.trim().length());//2
            System.out.println(test3.strip().length());//2

            String test4="测试　";//全角空白字符
            System.out.println(test4.trim().length());//3
            System.out.println(test4.strip().length());//2

            String test5="测试  ";//两个半角空白字符
            System.out.println(test5.trim().length());//2
            System.out.println(test5.strip().length());//2
        }
    }



    public static class Test6 {
        public static void main(String[] args) throws Exception {
            var classLoader = ClassLoader.getSystemClassLoader();
            var inputStream = classLoader.getResourceAsStream("myFile.txt");

            var tempFile = File.createTempFile("myFileCopy", "txt");
            try (var outputStream = new FileOutputStream(tempFile)) {
                inputStream.transferTo(outputStream);
            }

        }
    }

}
